package com.game_info.controller;

import java.util.List;

import com.game_info.service.GamesMsgService;
import com.game_info.utils.AjaxResult;
import com.game_info.vo.GamesMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 游戏留言表Controller
 *
 * @author ttj
 * @date 2020-04-14
 */
@Controller
@RequestMapping("/msg")
public class GamesMsgController {

    @Autowired
    private GamesMsgService gamesMsgService;


    /**
     * 传入空对象会查询所有留言列表
     * 根据任意字段查询留言评论列表
     */
    @PostMapping("/list.do")
    @ResponseBody
    public AjaxResult list(@RequestBody GamesMsg gamesMsg) {
        List<GamesMsg> list = gamesMsgService.selectGamesMsgList(gamesMsg);
        return AjaxResult.put(list, "查询评论列表方法执行成功", list.size());
    }

    /**
     * 根据gid(与msgBelong相匹配)获取游戏留言列表并分页
     *
     * @param msgBelong
     * @return
     */
    @GetMapping("/selectMsgsByGid.do")
    @ResponseBody
    public AjaxResult selectMsgsByGid(String msgBelong) {
        List<GamesMsg> list = gamesMsgService.selectMsgsByGid(msgBelong);
        return AjaxResult.put(list, "根据游戏id查询评论列表成功", list.size());
    }

    /**
     * 根据gid(与msgBelong相匹配)获取游戏留言列表并分页
     *
     * @param msgBelong
     * @return
     */
    @GetMapping("/selectMsgsLimitByGid.do")
    @ResponseBody
    public AjaxResult selectMsgsLimitByGid(String msgBelong, Integer page) {
        //入参小于0的情况.
        if (page <= 0) {
            page = 1;
        }
        Integer limit = (page - 1) * 10;//从第几条数据开始
        Integer maxPage = gamesMsgService.selectCountByMsgBelong(msgBelong) / 10 + 1;//总页数
        List<GamesMsg> list = gamesMsgService.selectMsgsLimitByGid(msgBelong, limit);
        return AjaxResult.put(list, "根据游戏id查询评论列表分页成功", maxPage);
    }

    /**
     * 分页查询所有留言
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/selectMsgLimit.do")
    @ResponseBody
    public AjaxResult selectMsgLimit(Integer page, @RequestParam(value = "limit", required = false) Integer limit) {
        if (limit == null) {
            limit = 5;//默认分页数据量5
        }
        //入参小于0的情况.
        if (page <= 0) {
            page = 1;
        }
        Integer max=gamesMsgService.getMsgCount();//获取留言总数
        List<GamesMsg> list=gamesMsgService.selectMsgLimit((page - 1) * limit, limit);
        return AjaxResult.put(list,"分页查询方法执行成功",max);
    }

    /**
     * 获取留言总数
     * @return
     */
    @GetMapping("/getMsgCount.do")
    @ResponseBody
    public Integer getMsgCount(){
        return gamesMsgService.getMsgCount();
    }

    /**
     * 根据关键词模糊查询全部字段信息
     *
     * @param keyword
     * @return
     */
    @GetMapping("/selectLikeKeyword.do")
    @ResponseBody
    public List<GamesMsg> selectLikeKeyword(String keyword) {
        return gamesMsgService.selectLikeKeyword(keyword);
    }

    /**
     * 获取当前游戏留言条数
     * @param msgBelong
     * @return
     */
    @GetMapping("/getGamesMsgCount.do")
    @ResponseBody
    public Integer getGamesMsgCount(String msgBelong){
        return gamesMsgService.selectCountByMsgBelong(msgBelong);
    }

    /**
     * 新增保存评论
     */
    @PostMapping("/add.do")
    @ResponseBody
    public AjaxResult addSave(@RequestBody GamesMsg gamesMsg) {
        return AjaxResult.put(gamesMsgService.insertGamesMsg(gamesMsg), "新增评论方法执行成功");
    }

    /**
     * 修改评论
     */
    @PostMapping("/edit.do")
    @ResponseBody
    public AjaxResult edit(@RequestBody GamesMsg gamesMsg) {

        return AjaxResult.put(gamesMsgService.updateGamesMsg(gamesMsg), "修改评论方法执行成功");
    }


    /**
     * 根据id删除评论
     */
    @PostMapping("/remove.do")
    @ResponseBody
    public AjaxResult remove(@RequestBody Integer id) {
        return AjaxResult.put(gamesMsgService.deleteGamesMsgById(id), "根据id删除评论方法执行成功");
    }
}
